package com.itheima.organization.controller;

import com.github.pagehelper.Page;
import com.itheima.aom.pojo.Aom;
import com.itheima.aom.pojo.AomOrder;
import com.itheima.aom.pojo.AomUser;
import com.itheima.entity.PageResult;
import com.itheima.entity.Result;
import com.itheima.entity.StatusCode;
import com.itheima.organization.requetbodypackage.annotation.MultiRequestBody;
import com.itheima.organization.service.AomOrderService;
import com.itheima.organization.service.AomService;
import com.itheima.utils.DateUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.util.Date;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/aom")
public class AomController {

    @Autowired
    private AomService aomService;

    @Autowired
    private AomOrderService aomOrderService;


    /**
     * 分页查询
     *
     * @param searchMap
     * @return
     */
    @GetMapping("/findPage/{page}/{size}")
    public Result findPage(@RequestParam Map<String, Object> searchMap, @PathVariable Integer page, @PathVariable Integer size) {
        Page<Aom> pageList;
        PageResult<Aom> pageResult;
        try {
            pageList = aomService.findPage(searchMap, page, size);
            pageResult =new PageResult((pageList.getTotal()), pageList.getResult());

        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR, "分页查询失败");
        }
        return new Result(true, StatusCode.OK, "分页查询成功", pageResult);
    }

    /**
     * 添加农业组织
     * 添加农业组织时添加订单信息
     *
     * @param aom
     * @param aomOrder
     * @return
     */
    @PostMapping("/add")
    public Result add(@MultiRequestBody Aom aom, @MultiRequestBody AomOrder aomOrder) {
        try {
            aomService.addAom(aom, aomOrder);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR, "添加农业组织失败");
        }
        return new Result(true, StatusCode.OK, "添加农业组织成功");
    }

    /**
     * 修改农业组织
     *
     * @param aom
     * @return
     */
    @PutMapping("/update")
    public Result update(@RequestBody Aom aom) {
        try {
            aomService.updateAom(aom);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(true, StatusCode.ERROR, "修改农业组织失败");
        }
        return new Result(true, StatusCode.OK, "修改农业组织成功");
    }

    /**
     * 逻辑删除农业组织
     *
     * @param aom
     * @return
     */
    @DeleteMapping("/delete")
    public Result delete(@RequestBody Aom aom) {

        try {
            //设置状态为未激活
            aom.setStatus("1");
            //查询当前农业组织的服务时间是否到期,未到期不可逻辑删除用户
            List<Map> list = aomService.selectByAomId(aom.getId());
            //获取数据库中订单服务的结束日期
            Long amoorderid = (Long) list.get(0).get("amoorderid");
            AomOrder aomOrder = aomOrderService.findById(amoorderid);
            String endtime = aomOrder.getEndtime();
            Date date = DateUtils.stringToDate(endtime);
            //判断是否过期
            if (new Date().after(date)) {
                aomService.updateAom(aom);
                return new Result(true, StatusCode.OK, "删除农业组织成功");
            } else {
                return new Result(true, StatusCode.ERROR, "删除农业组织失败,服务未到期");
            }
        } catch (ParseException e) {
            e.printStackTrace();
            return new Result(true, StatusCode.ERROR, "删除农业组织失败");
        }
    }
}
